package com.resou.reader.search.adpter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.bookshelf.readhistory.datasupport.SearchHistory;
import com.resou.reader.search.SearchPresenter;
import com.resou.reader.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索之前的RecyclerView
 * Created by ${Garrett} on 2018/9/6.
 * Contact me krouky@outlook.com
 */
public class SearchBeforeAdapter extends RecyclerView.Adapter {

    private static int TYPE_SIMPLE_TEXT = 0;
    private static int TYPE_SEARCH_HOT = 1;
    private static int TYPE_SEARCH_HISTORY = 3;

    private List<Object> mList = new ArrayList<>();

    private View.OnClickListener onClickListener;

    public SearchBeforeAdapter(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        initList();
    }

    private SearchBeforeInteractionListener mInteractionListener;

    public void setInteractionListener(SearchBeforeInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    private void initList() {
        mList.add(null);//大家都在搜标题
        mList.add(null);//hot
        mList.add(null);//搜索历史标题
    }

    public void setList(List<Object> list) {
        this.mList = list;
    }

    public List<Object> getList() {
        return mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//        if (i == TYPE_SIMPLE_TEXT) {
//            return new TextViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.simple_text_layout, viewGroup, false));
//        } else if (i == TYPE_SEARCH_HOT) {
//            return new HotViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_hot_layout, viewGroup, false));
//        } else {
//            return new HistoryViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_history_item, viewGroup, false));
//        }
        if (i == 0) {
            return new TextViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.simple_text_layout, viewGroup, false));
        } else if (i == 1) {
            return new HotViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_hot_layout, viewGroup, false));
        } else if (i == 2) {
            return new TextViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.simple_text_layout, viewGroup, false));
        } else {
            return new HistoryViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_history_item, viewGroup, false));
        }
    }

    //从热搜列表中选出的文字
    private List<String> mAssignList;

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        switch (i) {
            case 0:
                ((TextViewHolder) viewHolder).tv.setText(viewHolder.itemView.getContext().getString(R.string.everyone_in_the_search));
                ((TextViewHolder) viewHolder).rightTv.setOnClickListener((v) -> {
                    isChangeHotList = true;
                    mInteractionListener.onChangeClicked();
                });
                if (hots != null && hots.size() > SearchPresenter.HOT_SIZE_MAX) {
                    ((TextViewHolder) viewHolder).rightTv.setVisibility(View.VISIBLE);
                    ((TextViewHolder) viewHolder).rightTv.setOnClickListener(v -> {
                        SearchBeforeAdapter.this.notifyItemChanged(1);
                    });
                }
//                else {
//                    ((TextViewHolder) viewHolder).rightTv.setVisibility(View.GONE);
//                }
                break;
            case 1:
                FlowLayout flowLayout = ((HotViewHolder) viewHolder).flowLayout;
                if (hots != null) {
                    flowLayout.removeAllViews();
                    if (isChangeHotList || mAssignList == null) {
                        mAssignList = getAssignList(hots);
                        for (int k = 0; k < mAssignList.size(); k++) {
                            TextView tv = (TextView) LayoutInflater.from(viewHolder.itemView.getContext()).inflate(R.layout.hot_text_item, flowLayout, false);
                            tv.setText(mAssignList.get(k));
                            flowLayout.addView(tv);
                            tv.setOnClickListener(onClickListener);
                        }
                        isChangeHotList = false;
                    } else {
                        for (int k = 0; k < mAssignList.size(); k++) {
                            TextView tv = (TextView) LayoutInflater.from(viewHolder.itemView.getContext()).inflate(R.layout.hot_text_item, flowLayout, false);
                            tv.setText(mAssignList.get(k));
                            flowLayout.addView(tv);
                            tv.setOnClickListener(onClickListener);
                        }
                        isChangeHotList = false;
                    }
//                    List<String> tempList = getAssignList(hots);
//                    flowLayout.removeAllViews();
                }
                break;
            case 2:
                ((TextViewHolder) viewHolder).tv.setText(viewHolder.itemView.getContext().getString(R.string.search_history));
                ((TextViewHolder) viewHolder).rightTv.setText(R.string.delete);
                if (histories != null && histories.size() > 0) {
                    ((TextViewHolder) viewHolder).rightTv.setVisibility(View.VISIBLE);
                    ((TextViewHolder) viewHolder).tv.setVisibility(View.VISIBLE);
                    ((TextViewHolder) viewHolder).rightTv.setOnClickListener(v -> {
                        mInteractionListener.onDeleteClicked();
                        onClickListener.onClick(v);
                    });
                } else {
                    ((TextViewHolder) viewHolder).rightTv.setVisibility(View.GONE);
                    ((TextViewHolder) viewHolder).tv.setVisibility(View.GONE);
                }

                break;
            case 3:
            default:
                SearchHistory searchHistory = (SearchHistory) mList.get(i);
                if (searchHistory == null) {
                    break;
                }
                String history = searchHistory.getHistory();
                if (!TextUtils.isEmpty(history)) {
                    ((HistoryViewHolder) viewHolder).historyItemText.setText(history);
                }
                break;
        }

    }

//    @Override
//    public int getItemViewType(int position) {
//        switch (position) {
//            case 0:
//                return TYPE_SIMPLE_TEXT;
//            case 1:
//                return TYPE_SEARCH_HOT;
//            case 2:
//                return TYPE_SIMPLE_TEXT;
//            case 3:
//            default:
//                return TYPE_SEARCH_HISTORY;
//        }
//    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }
        if (position == 1) {
            return 1;
        }
        if (position == 2) {
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class TextViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        TextView rightTv;

        TextViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.simple_text);
            rightTv = itemView.findViewById(R.id.right_text);
        }
    }

    class HotViewHolder extends RecyclerView.ViewHolder {

        FlowLayout flowLayout;

        HotViewHolder(@NonNull View itemView) {
            super(itemView);
            flowLayout = itemView.findViewById(R.id.search_hot_group);
        }
    }

    class HistoryViewHolder extends RecyclerView.ViewHolder {

        FrameLayout historyView;
        TextView historyItemText;

        HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            historyView = itemView.findViewById(R.id.history_item_group);
            historyItemText = itemView.findViewById(R.id.history_item_text);
            historyView.setOnClickListener(v -> {
                v.setTag(historyItemText.getText());
                onClickListener.onClick(v);
            });
        }
    }

    private static int start = 0;

    //从第start位置取
    private List<String> getAssignList(List<String> list) {
        if (list.size() <= SearchPresenter.HOT_SIZE_MAX) {
            return list;
        }
        List<String> result = new ArrayList<>();
        if (start >= list.size()) {
            start -= list.size();
        }
        while (result.size() < SearchPresenter.HOT_SIZE_MAX) {
            if (start == list.size()) {
                start = 0;
            }
            result.add(list.get(start));
            start++;
        }
        return result;
    }

    private List<String> hots;
    private List<SearchHistory> histories;
    private boolean isChangeHotList;

    public void setHotList(List<String> list) {
        hots = list;
        mList.set(1, list);
        isChangeHotList = true;
//        notifyItemChanged(0);
        notifyItemChanged(1);
    }

    public void setHistoryList(List<SearchHistory> list) {
        histories = list;
        mList.clear();
        mList.add(null);
        mList.add(hots);
        mList.add(null);
        isChangeHotList = true;
        if (list == null || list.size() == 0) {
            notifyDataSetChanged();
        } else {
            mList.addAll(list);
            notifyItemRangeChanged(2, list.size());
        }
    }

//    public void setHistoryList(List<SearchHistory> list) {
//        mList.clear();
//        mList.add(null);
//        mList.add(hots);
//        mList.add(null);
//        mList.addAll(list);
//        notifyDataSetChanged();
////        notifyItemRangeChanged(2, list.size());
////        if (mList.size() >= 3) {
////            for (int i = 3; i < mList.size(); i++) {
////                mList.remove(mList.get(i));
////            }
////        }
////        notifyDataSetChanged();
////        histories = list;
////        mList.addAll(histories);
//////        if (list == null || list.size() == 0) {
//////            notifyDataSetChanged();
//////        } else {
//////            mList.addAll(list);
//////            notifyItemRangeChanged(0, mList.size() );
//////        }
////        notifyItemRangeChanged(2, list.size());
//    }

    public interface SearchBeforeInteractionListener {
        void onChangeClicked();

        void onDeleteClicked();
    }
}
